package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.form.FormUserFull;
import com.huahang.fashionpress.service.impl.UserInfoServiceImpl;
import com.huahang.fashionpress.util.Md5Util;
import com.huahang.fashionpress.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 控制用户个人信息页面
 * @author : 王翰聪
 * @since : 2019/08/22
 * @version : 1.0
 */

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoServiceImpl userInfoServiceImpl;

    @RequestMapping("/")
    public String sendUserInfoView(ModelAndView modelAndView, HttpSession session){
        int id = (int)session.getAttribute("userID");
        UserInfoVO userInfoVO = userInfoServiceImpl.getUserInfo(id);
        modelAndView.addObject(userInfoVO);
        return "/";
    }
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(ModelAndView modelAndView, String username, String phone, String email, HttpSession session){
        int id = (int) session.getAttribute("userID");
        FormUserFull formUserFull = new FormUserFull();
        formUserFull.setUserUsername(username);
        formUserFull.setPhone(phone);
        formUserFull.setEMail(email);
        userInfoServiceImpl.updateUser(id, formUserFull);
        UserInfoVO userInfoVO = userInfoServiceImpl.getUserInfo(id);
        modelAndView.addObject(userInfoVO);
        return "/";
    }


    @RequestMapping("/updatePassword")
    public String updatePassword(ModelAndView modelAndView, HttpSession session, String password){
        int id = (int) session.getAttribute("userID");
        //将密码加密
        String pwd = Md5Util.getMd5String(password);
        userInfoServiceImpl.updatePassword(id, pwd);

        //修改密码后返回登录页面
        return "/login";
    }
}
